package com.google.code.rbta.library;

import org.apache.http.HttpVersion;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;
import android.webkit.WebView;

class Internet implements ComponentCallbacks
{
    private static final String TAG      = Internet.class.getSimpleName();
    private static final int    TIME_OUT = 60 * 1000;
    private static Internet     sInstance;
    
    private String              mUserAgent;
    private DefaultHttpClient   mHttpClient;
    private Context             mContext;

    public static Internet getInstance()
    {
        if( sInstance == null )
        {
            Log.w( TAG, "You must call createInstance to init your internet portal." );
            return null;
        }
        return sInstance;
    }

    public static Internet createInstance( Context _cxt )
    {
        if( sInstance == null )
        {
            synchronized( Internet.class )
            {
                if( sInstance == null )
                {
                    sInstance = new Internet( _cxt ); 
                }
            }
        }
        return sInstance;
    }

    private Internet( Context _cxt )
    {
        mContext = _cxt;
        mUserAgent = new WebView( mContext ).getSettings().getUserAgentString();
        mHttpClient = createHttpClient();
    }

    public String getUserAgent()
    {
        return mUserAgent;
    }

    public void shutdownNow()
    {
        if( mHttpClient != null && mHttpClient.getConnectionManager() != null )
        {
            mHttpClient.getConnectionManager().shutdown();
        }
    }

    private DefaultHttpClient createHttpClient()
    {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout( params, TIME_OUT );
        HttpConnectionParams.setSoTimeout( params, TIME_OUT );
        HttpProtocolParams.setVersion( params, HttpVersion.HTTP_1_1 );
        HttpProtocolParams.setContentCharset( params, HTTP.DEFAULT_CONTENT_CHARSET );
        HttpProtocolParams.setUseExpectContinue( params, true );

        SchemeRegistry schReg = new SchemeRegistry();
        schReg.register( new Scheme( "http", PlainSocketFactory.getSocketFactory(), 80 ) );
        schReg.register( new Scheme( "https", SSLSocketFactory.getSocketFactory(), 443 ) );
        ClientConnectionManager conMgr = new ThreadSafeClientConnManager( params, schReg );

        DefaultHttpClient httpclient = new DefaultHttpClient( conMgr, params );
        return httpclient;
    }

    @Override
    public void onConfigurationChanged( Configuration _newConfig )
    {

    }

    @Override
    public void onLowMemory()
    {
        shutdownNow();
    }

    public DefaultHttpClient getHttpClient()
    {
        return mHttpClient;
    }
}
